package gov.nasa.anml.utility;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import static java.util.Arrays.*;

/** Stolen ("adapted") from HashMap
 * 
 * C-style; shoot-yourself-in-the-foot-style
 * 
 * Fails slowly, does not attempt to correctly serialize if sub-classed.
 * i.e.: "Use at your own risk."
 * 
 * 
 * @see HashMap
 *
 * @param <V>
 */

public class cArrHashMap<V>
implements Cloneable, Serializable, cArrMap<V>
{

	/**
	 * The default initial capacity - MUST be a power of two.
	 */
	static final int DEFAULT_INITIAL_CAPACITY = 16;

	/**
	 * The maximum capacity, used if a higher value is implicitly specified
	 * by either of the constructors with arguments.
	 * MUST be a power of two <= 1<<30.
	 */
	static final int MAXIMUM_CAPACITY = 1 << 30;

	/**
	 * The load factor used when none specified in constructor.
	 */
	static final float DEFAULT_LOAD_FACTOR = 0.75f;

	/**
	 * The table, resized as necessary. Length MUST Always be a power of two.
	 */
	transient Entry[] table;

	/**
	 * The number of key-value mappings contained in this map.
	 */
	transient int size;

	/**
	 * The next size value at which to resize (capacity * load factor).
	 * @serial
	 */
	int threshold;

	/**
	 * The load factor for the hash table.
	 *
	 * @serial
	 */
	final float loadFactor;

	// Views
	transient EntrySet entrySet;
	transient KeySet keySet;
	transient Values values;
	
	/**
	 * Constructs an empty <tt>iHashMap</tt> with the specified initial
	 * capacity and load factor.
	 *
	 * @param  initialCapacity the initial capacity
	 * @param  loadFactor      the load factor
	 * @throws IllegalArgumentException if the initial capacity is negative
	 *         or the load factor is nonpositive
	 */
	public cArrHashMap(int initialCapacity, float loadFactor) {
		if (initialCapacity < 0)
			throw new IllegalArgumentException("Illegal initial capacity: " +
					initialCapacity);
		if (initialCapacity > MAXIMUM_CAPACITY)
			initialCapacity = MAXIMUM_CAPACITY;
		if (loadFactor <= 0 || Float.isNaN(loadFactor))
			throw new IllegalArgumentException("Illegal load factor: " +
					loadFactor);

		// Find a power of 2 >= initialCapacity
		int capacity = 1;
		while (capacity < initialCapacity)
			capacity <<= 1;

		this.loadFactor = loadFactor;
		threshold = (int)(capacity * loadFactor);
		table = new Entry<?>[capacity];
	}

	/**
	 * Constructs an empty <tt>iHashMap</tt> with the specified initial
	 * capacity and the default load factor (0.75).
	 *
	 * @param  initialCapacity the initial capacity.
	 * @throws IllegalArgumentException if the initial capacity is negative.
	 */
	public cArrHashMap(int initialCapacity) {
		this(initialCapacity, DEFAULT_LOAD_FACTOR);
	}

	/**
	 * Constructs an empty <tt>iHashMap</tt> with the default initial capacity
	 * (16) and the default load factor (0.75).
	 */
	public cArrHashMap() {
		this.loadFactor = DEFAULT_LOAD_FACTOR;
		threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
		table = new Entry<?>[DEFAULT_INITIAL_CAPACITY];
	}

	/**
	 * Constructs a new <tt>iHashMap</tt> with the same mappings as the
	 * specified <tt>Map</tt>.  The <tt>iHashMap</tt> is created with
	 * default load factor (0.75) and an initial capacity sufficient to
	 * hold the mappings in the specified <tt>Map</tt>.
	 *
	 * @param   m the map whose mappings are to be placed in this map
	 * @throws  NullPointerException if the specified map is null
	 */
	public <W extends V> cArrHashMap(cArrHashMap<W> m) {
		this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
				DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
		putAllForCreate(m);
	}

	public cArrHashMap(Map<List<? extends SimpleCharacter>, ? extends V> m) {
		this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
				DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
		putAllSimpleCharacter(m);
	}

	public cArrHashMap(SimpleString exampleKey, Map<? extends SimpleString, ? extends V> m) {
		this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
				DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
		putAllSimpleString(m);
	}

	public cArrHashMap(String exampleKey, Map<String, ? extends V> m) {
		this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
				DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
		putAllString(m);
	}
	
	public cArrHashMap(Character exampleKey, Map<List<Character>, ? extends V> m) {
		this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
				DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
		putAllCharacter(m);
	}
	
	public cArrHashMap(char[] exampleKey, Map<char[],?extends V> m) {
		this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
				DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
		putAllArray(m);
	}

	// internal utilities

	/**
	 * Applies the HashMap hash function to the key; ultimately
	 * producing the same index as a HashMap<ArrayList<Character>> would.
	 * 
	 * char[].hashCode() is just Object.hashCode(), i.e., memory address.
	 * 
	 * @see HashMap#hash(int h)
	 * @see HashMap#indexFor(int k, int length)
	 * @see java.util.Arrays#hashCode(char[])
	 * @return index for key
	 */
	static int indexFor(char[] k, int length) {
		int h = Arrays.hashCode(k);
		h ^= (h >>> 20) ^ (h >>> 12);
		return (h ^ (h >>> 7) ^ (h >>> 4)) & (length-1);
	}

	/**
	 * @return the number of key-value mappings in this map
	 */
	public int size() {
		return size;
	}

	/**
	 * @return <tt>true</tt> if this map contains no key-value mappings
	 */
	public boolean isEmpty() {
		return size == 0;
	}

	/**
	 * Returns the value to which the specified key is mapped,
	 * or {@code null} if this map contains no mapping for the key.
	 *
	 * @see #put(int, V)
	 */
	public V get(char[] key) {
		//int hash = hash(key);
		for (Entry<V> e = table[indexFor(key, table.length)];
		e != null;
		e = e.next) {
			// wtf?  testing to see if the hash matches??
			// ahh....two levels of hashing -- keys to hashes to indices
			// sort of debatable whether e.hash == hash saves any time
			// for integer keys...
			// just doing simple key comparison...

			//if (e.hash == hash && e.key == key)
			if (Arrays.equals(e.key,key))
				return e.value;
		}
		return null;
	}

	/** Superfluous, but saves a few keystrokes.
	 * 
	 * @param   key   The key whose presence in this map is to be tested
	 * @return <tt>true</tt> if this map contains a mapping for the specified
	 * key.
	 */
	public final boolean containsKey(char[] key) {
		return get(key) != null;
	}

	/**
	 * Returns the entry associated with the specified key in the
	 * iHashMap.  Returns null if the iHashMap contains no mapping
	 * for the key.
	 */
	final Entry<V> getEntry(char[] key) {
		//int hash = hash(key);
		for (Entry<V> e = table[indexFor(key, table.length)];
		e != null;
		e = e.next) {
			if (e.key == key)
				return e;
		}
		return null;
	}


	/**
	 * Associates the specified value with the specified key in this map.
	 * If the map previously contained a mapping for the key, the old
	 * value is replaced.
	 * 
	 * If the specified value is null, the mapping is deleted -- null
	 * cannot be mapped to.
	 * 
	 * If the key changes, everything will break.  Well, not if the
	 * hashCode() somehow remains the same.  
	 * The same is true of HashMap<ArrayList<Character>,V>.
	 *
	 * @param key key with which the specified value is to be associated
	 * @param value value to be associated with the specified key
	 * @return the previous value associated with <tt>key</tt>, or
	 *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
	 */
	public V put(char[] key, V value) {
		if (value == null)
			return remove(key);
		//int hash = hash(key);
		int i = indexFor(key, table.length);
		Entry<V> e = table[i];
		V oldValue;
		if (e == null) {
			table[i] = new Entry<V>(key,value);
			++size;
			return null;
		}
		if (Arrays.equals(e.key,key)) {
			oldValue = e.value;
			e.value = value;
			return oldValue;
		}
		if (e.next != null) {
			do {
				e = e.next;
				if (Arrays.equals(e.key,key)) {
					oldValue = e.value;
					e.value = value;
					return oldValue;
				}            	
			}
			while(e.next != null);
		}
		e.next = new Entry<V>(key,value);
		++size;
		return null;
	}

	/**
	 * This method is used instead of put by constructors and
	 * pseudoconstructors (clone, readObject).  It does not resize the table,
	 * check for comodification, etc.  It calls createEntry rather than
	 * addEntry.
	 * 
	 * @deprecated just use createEntry
	 */
	private void putForCreate(char[] key, V value) {
		//int hash = hash(key);
		int i = indexFor(key, table.length);

		createEntry(i, key, value);
	}

	// but note that such Maps are broken
	private <W extends V> 
	void putAllArray(Map<char[],W> m) {
		int i,l=table.length;
		char[] k=null;
		for (Map.Entry<char[],W> e : m.entrySet()) {
			k = e.getKey();
			i=indexFor(k,l);
			createEntry(i,k,e.getValue());
		}
	}
	// have to copy, as can't access the backing array, which
	// is anyways of the wrong type
	private <K extends List<Character>, W extends V> 
	void putAllCharacter(Map<K,W> m) {
		int i,l=table.length,keyLen;
		char[] k=null;
		K key;
		for (Map.Entry<K,W> e : m.entrySet()) {
			i=0;
			key = e.getKey();
			keyLen = key.size();
			k = new char[keyLen];
			for(Character s : key)
				k[i++] = s.charValue();
			i=indexFor(k,l);
			createEntry(i,k,e.getValue());
		}
	}
	// have to copy, as can't access the backing array, which
	// is anyways of the wrong type
	private <K extends List<? extends SimpleCharacter>, W extends V> 
	void putAllSimpleCharacter(Map<K,W> m) {
		int i,l=table.length,keyLen;
		char[] k=null;
		K key;
		for (Map.Entry<K,W> e : m.entrySet()) {
			i=0;
			key = e.getKey();
			keyLen = key.size();
			k = new char[keyLen];
			for(SimpleCharacter s : key)
				k[i++] = s.v;
			i=indexFor(k,l);
			createEntry(i,k,e.getValue());
		}
	}
	// uses the backing array.  dangerous!
	private <K extends SimpleString, W extends V> 
	void putAllSimpleString(Map<K,W> m) {
		int i,l=table.length;
		char[] k=null;
		for (Map.Entry<K,W> e : m.entrySet()) {
			k = e.getKey().v;
			i=indexFor(k,l);
			createEntry(i,k,e.getValue());
		}
	}
	// have to copy, as can't access the backing array
	private <W extends V> 
	void putAllString(Map<String,W> m) {
		int i,l=table.length;
		char[] k=null;
		String key;
		for (Map.Entry<String,W> e : m.entrySet()) {
			i=0;
			key = e.getKey();
			k = key.toCharArray();
			i=indexFor(k,l);
			createEntry(i,k,e.getValue());
		}
	}
	// shares keys -- dangerous
	private <W extends V> 
	void putAllForCreate(cArrHashMap<W> m) {
		int i,l=table.length;
		char[] k;
		for (Entry<W> e : m.table) {
			Entry<W> f;
			for (f=e;f!=null;f=f.next) {
				k = f.key;
				i=indexFor(k,l);
				createEntry(i,k,f.value);
			}
		}
	}

	/**
	 * Rehashes the contents of this map into a new array with a
	 * larger capacity.  This method is called automatically when the
	 * number of keys in this map reaches its threshold.
	 *
	 * If current capacity is MAXIMUM_CAPACITY, this method does not
	 * resize the map, but sets threshold to Integer.MAX_VALUE.
	 * This has the effect of preventing future calls.
	 *
	 * @param newCapacity the new capacity, MUST be a power of two;
	 *        must be greater than current capacity unless current
	 *        capacity is MAXIMUM_CAPACITY (in which case value
	 *        is irrelevant).
	 */
	void resize(int newCapacity) {
		Entry[] oldTable = table;
		int oldCapacity = oldTable.length;
		if (oldCapacity == MAXIMUM_CAPACITY) {
			threshold = Integer.MAX_VALUE;
			return;
		}

		Entry[] newTable = new Entry<?>[newCapacity];
		transfer(newTable);
		table = newTable;
		threshold = (int)(newCapacity * loadFactor);
	}

	/**
	 * Transfers all entries from current table to newTable.
	 */
	void transfer(Entry[] newTable) {
		Entry[] src = table;
		int newCapacity = newTable.length;
		for (int j = 0; j < src.length; j++) {
			Entry<V> e = src[j];
			if (e != null) {
				src[j] = null;
				do {
					Entry<V> next = e.next;
					int i = indexFor(e.key, newCapacity);
					e.next = newTable[i];
					newTable[i] = e;
					e = next;
				} while (e != null);
			}
		}
	}

	/**
	 * Copies all of the mappings from the specified map to this map.
	 * These mappings will replace any mappings that this map had for
	 * any of the keys currently in the specified map.
	 *
	 * @param m mappings to be stored in this map
	 * @throws NullPointerException if the specified map is null
	 */
	public <W extends V> void putAll(cArrMap<W> m) {
		int numKeysToBeAdded = m.size();
		if (numKeysToBeAdded == 0)
			return;

		/*
		 * Expand the map if the map if the number of mappings to be added
		 * is greater than or equal to threshold.  This is conservative; the
		 * obvious condition is (m.size() + size) >= threshold, but this
		 * condition could result in a map with twice the appropriate capacity,
		 * if the keys to be added overlap with the keys already in this map.
		 * By using the conservative calculation, we subject ourself
		 * to at most one extra resize.
		 */
		if (numKeysToBeAdded > threshold) {
			int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
			if (targetCapacity > MAXIMUM_CAPACITY)
				targetCapacity = MAXIMUM_CAPACITY;
			int newCapacity = table.length;
			while (newCapacity < targetCapacity)
				newCapacity <<= 1;
			if (newCapacity > table.length)
				resize(newCapacity);
		}
		// scan the table instead?
		// probably not...hashing can work differently for different sizes
		for (cArrMap.Entry<W> e : m.entrySet()) {
			put(e.getKey(), e.getValue());
		}
	}

	/**
	 * Removes the mapping for the specified key from this map if present.
	 *
	 * @param  key key whose mapping is to be removed from the map
	 * @return the previous value associated with <tt>key</tt>, or
	 *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
	 */
	public V remove(char[] key) {
		int i = indexFor(key, table.length);
		Entry<V> e = table[i];

		if (e == null)
			return null;
		if (e.key == key) {
			--size;
			table[i] = e.next;
			return e.value;
		}

		Entry<V> prev, next=e.next;
		if (next != null) {
			do {
				prev = e;
				e = next;
				if (e.key == key) {
					--size;
					prev.next = next;
					return e.value;
				}
				next = e.next;
			}
			while (next != null);
		}
		return null;
	}

	/**
	 * Removes and returns the entry associated with the specified key
	 * in the iHashMap.  Returns null if the iHashMap contains no mapping
	 * for this key.
	 * 
	 * Only for inlining purposes?
	 * @see #remove(int)
	 */
	final Entry<V> removeEntryForKey(char[] key) {
		//int hash = hash(key);
		int i = indexFor(key, table.length);
		Entry<V> e = table[i];

		if (e == null)
			return null;
		if (e.key == key) {
			--size;
			table[i] = e.next;
			return e;
		}

		Entry<V> prev, next=e.next;
		if (next != null) {
			do {
				prev = e;
				e = next;
				if (e.key == key) {
					--size;
					prev.next = next;
					return e;
				}
				next = e.next;
			}
			while (next != null);
		}
		return null;
	}


	/**
	 * Removes all of the mappings from this map.
	 * The map will be empty after this call returns.
	 */
	public void clear() {
		Entry[] tab = table;
		for (int i = 0; i < tab.length; i++)
			tab[i] = null;
		size = 0;
	}

	/**
	 * Returns <tt>true</tt> if this map maps one or more keys to the
	 * specified value.
	 *
	 * @param v value whose presence in this map is to be tested
	 * @return <tt>true</tt> if this map maps one or more keys to the
	 *         specified value
	 */
	public boolean containsObject(Object v) {
		return containsValue((V)v);
	}
	public boolean containsValue(V value) {
		if (value == null)
			return false;

		Entry[] tab = table;
		for (int i = 0; i < tab.length ; i++)
			for (Entry<V> e = tab[i] ; e != null ; e = e.next)
				if (value.equals(e.value))
					return true;
		return false;
	}

	/**
	 * Returns a deep-ish copy of this <tt>iHashMap</tt> instance;
	 * the values are not cloned, but the references are.
	 * 
	 * Changing a key-value mapping in a clone does not alter the original --
	 * but directly altering a value is reflected in all references to that
	 * value, e.g., in all clones. 
	 * 
	 * The keys are primitives and therefore copied.
	 *
	 * @return a shallow copy of this map
	 */
	public cArrHashMap<V> clone() {
		cArrHashMap<V> result = null;
		try {
			result = (cArrHashMap<V>)super.clone();
		} catch (CloneNotSupportedException e) {
			// assert false;
		}
		result.table = new Entry<?>[table.length];
		result.entrySet = null;
		result.size = 0;
		result.putAllForCreate(this);

		return result;
	}

	public static class Entry<V> implements cArrMap.Entry<V> {
		public final char[] key;
		public V value;
		Entry<V> next;

		/**
		 * Creates new entry.
		 */
		Entry(char[] key, V value) {
			this.key = key;
			this.value = value;
		}
		Entry(char[] key, V value, Entry<V> next) {
			this.key = key;
			this.value = value;
			this.next = next;
		}

		public final char[] getKey() {
			return key;
		}

		public final V getValue() {
			return value;
		}

		public final V setValue(V newValue) {
			V oldValue = value;
			value = newValue;
			return oldValue;
		}

		public final boolean equals(Object o) {
			if (!(o instanceof Entry))
				return false;
			Entry<V> e = (Entry<V>)o;
			
			if (key == e.key) {
				V v1 = value, v2 = e.value;
				if (v1 == v2 || (v1 != null && v1.equals(v2)))
					return true;
			}
			return false;
		}

		public final int hashCode() {
			return (Arrays.hashCode(key)) ^ (value==null ? 0 : value.hashCode());
		}

		public final String toString() {
			return key + "=" + value;
		}

	}

	/**
	 * Used when creating entries
	 * as part of Map construction or "pseudo-construction" (cloning,
	 * deserialization).  This version needn't worry about resizing the table.
	 *
	 * Subclass overrides this to alter the behavior of iHashMap(Map),
	 * iHashMap(iHashMap), clone, and readObject.
	 */
	Entry<V> createEntry(int bucketIndex, char[] key, V value) {
		Entry<V> e = table[bucketIndex];
		table[bucketIndex] = new Entry<V>(key, value, e);
		size++;
		return e;
	}

	private abstract class HashIterator<E> implements Iterator<E> {
		Entry<V> next;	// next entry to return
		int index;		// current slot
		Entry<V> current;	// current entry

		HashIterator() {
			if (size > 0) { // advance to first entry
				Entry[] t = table;
				do {} while (index < t.length && (next = t[index++]) == null);
			}
		}

		public final boolean hasNext() {
			return next != null;
		}

		final Entry<V> nextEntry() {
			Entry<V> e = current = next;
			if (e == null)
				throw new NoSuchElementException();

			if ((next = e.next) == null) {
				Entry[] t = table;
				do {} while (index < t.length && (next = t[index++]) == null);
			}
			return e;
		}

		public void remove() {
			if (current == null)
				throw new IllegalStateException();
			char[] k = current.key;
			current = null;
			removeEntryForKey(k);
		}

	}

	private final class ValueIterator extends HashIterator<V> {
		public V next() {
			return nextEntry().value;
		}
	}

	private final class KeyIterator extends HashIterator<char[]> {
		public char[] next() {
			return nextEntry().key;
		}
	}

	private final class EntryIterator extends HashIterator<cArrMap.Entry<V>> {
		public cArrMap.Entry<V> next() {
			return nextEntry();
		}
	}

	// Subclass overrides these to alter behavior of views' iterator() method
	Iterator<char[]> newKeyIterator()   {
		return new KeyIterator();
	}
	Iterator<V> newValueIterator()   {
		return new ValueIterator();
	}
	Iterator<cArrMap.Entry<V>> newEntryIterator()   {
		return new EntryIterator();
	}



	/**
	 * Returns a {@link Set} view of the keys contained in this map.
	 * The set is backed by the map, so changes to the map are
	 * reflected in the set, and vice-versa.  If the map is modified
	 * while an iteration over the set is in progress (except through
	 * the iterator's own <tt>remove</tt> operation), the results of
	 * the iteration are undefined.  The set supports element removal,
	 * which removes the corresponding mapping from the map, via the
	 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
	 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
	 * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
	 * operations.
	 * 
	 * Nor does it allow modifying the wrapped keys.
	 */
	public Set<char[]> keySet() {
		Set<char[]> ks = keySet;
		return (ks != null ? ks : (keySet = new KeySet()));
	}

	private final class KeySet extends AbstractSet<char[]> {
		public Iterator<char[]> iterator() {
			return newKeyIterator();
		}
		public int size() {
			return size;
		}
		public boolean contains(Object o) {
			if (o instanceof char[])
				return contains((char[])o);
			return false;
		}
		public boolean contains(char[] k) {
			return containsKey(k);
		}
		public boolean remove(Object o) {
			if (o instanceof char[])
				return remove((char[])o);
			return false;
		}
		public boolean remove(char[] k) {
			return cArrHashMap.this.removeEntryForKey(k) != null;
		}
		public void clear() {
			cArrHashMap.this.clear();
		}
	}

	/**
	 * Returns a {@link Collection} view of the values contained in this map.
	 * The collection is backed by the map, so changes to the map are
	 * reflected in the collection, and vice-versa.  If the map is
	 * modified while an iteration over the collection is in progress
	 * (except through the iterator's own <tt>remove</tt> operation),
	 * the results of the iteration are undefined.  The collection
	 * supports element removal, which removes the corresponding
	 * mapping from the map, via the <tt>Iterator.remove</tt>,
	 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
	 * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
	 * support the <tt>add</tt> or <tt>addAll</tt> operations.
	 */
	public Collection<V> values() {
		Collection<V> vs = values;
		return (vs != null ? vs : (values = new Values()));
	}

	private final class Values extends AbstractCollection<V> {
		public Iterator<V> iterator() {
			return newValueIterator();
		}
		public int size() {
			return size;
		}
		public boolean contains(Object o) {
			return containsObject(o);
		}
		public void clear() {
			cArrHashMap.this.clear();
		}
	}

	/**
	 * Returns a {@link Set} view of the mappings contained in this map.
	 * The set is backed by the map, so changes to the map are
	 * reflected in the set, and vice-versa.  If the map is modified
	 * while an iteration over the set is in progress (except through
	 * the iterator's own <tt>remove</tt> operation, or through the
	 * <tt>setValue</tt> operation on a map entry returned by the
	 * iterator) the results of the iteration are undefined.  The set
	 * supports element removal, which removes the corresponding
	 * mapping from the map, via the <tt>Iterator.remove</tt>,
	 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
	 * <tt>clear</tt> operations.  It does not support the
	 * <tt>add</tt> or <tt>addAll</tt> operations.
	 *
	 * @return a set view of the mappings contained in this map
	 */
	public Set<cArrMap.Entry<V>> entrySet() {
		Set<cArrMap.Entry<V>> es = entrySet;
		return es != null ? es : (entrySet = new EntrySet());
	}

	private final class EntrySet extends AbstractSet<cArrMap.Entry<V>> {
		public Iterator<cArrMap.Entry<V>> iterator() {
			return newEntryIterator();
		}
		public boolean contains(Object o) {
			if (!(o instanceof Entry))
				return false;
			Entry<V> e = (Entry<V>) o;
			Entry<V> candidate = getEntry(e.key);
			return candidate != null && candidate.equals(e);
		}
		public boolean remove(Object o) {
			if (o instanceof Entry) {
				Entry<V> e = (Entry<V>) o;
				return cArrHashMap.this.remove(e.key) != null;
			}
			return false;
		}
		public int size() {
			return size;
		}
		public void clear() {
			cArrHashMap.this.clear();
		}
	}

	/**
	 * Save the state of the <tt>iHashMap</tt> instance to a stream (i.e.,
	 * serialize it).
	 *
	 * @serialData The <i>capacity</i> of the iHashMap (the length of the
	 *		   bucket array) is emitted (int), followed by the
	 *		   <i>size</i> (an int, the number of key-value
	 *		   mappings), followed by the key (Object) and value (Object)
	 *		   for each key-value mapping.  The key-value mappings are
	 *		   emitted in no particular order.
	 */
	private void writeObject(java.io.ObjectOutputStream s)
	throws IOException
	{
		Iterator<cArrMap.Entry<V>> i =
			(size > 0) ? entrySet().iterator() : null;

			// Write out the threshold, loadfactor, and any hidden stuff
			s.defaultWriteObject();

			// Write out number of buckets
			s.writeInt(table.length);

			// Write out size (number of Mappings)
			s.writeInt(size);

			// Write out keys and values (alternating)
			if (i != null) {
				while (i.hasNext()) {
					cArrMap.Entry<V> e = i.next();
					s.writeObject(e.getKey());
					s.writeObject(e.getValue());
				}
			}
	}

	private static final long serialVersionUID = 362498820763181265L;

	/**
	 * Reconstitute the <tt>cArrHashMap</tt> instance from a stream (i.e.,
	 * deserialize it).
	 */
	private void readObject(java.io.ObjectInputStream s)
	throws IOException, ClassNotFoundException
	{
		// Read in the threshold, loadfactor, and any hidden stuff
		s.defaultReadObject();

		// Read in number of buckets and allocate the bucket array;
		int numBuckets = s.readInt();
		table = new Entry<?>[numBuckets];


		// Read in size (number of Mappings)
		// hides this.size, and that is important since createEntry increments it
		int size = s.readInt();

		// Read the keys and values, and put the mappings in the iHashMap
		for (int i=0; i<size; i++) {
			char[] key = (char[]) s.readObject();
			V value = (V) s.readObject();
			createEntry(indexFor(key,numBuckets),key,value);
		}
	}

	// These methods are used when serializing HashSets
	int   capacity()     { return table.length; }
	float loadFactor()   { return loadFactor;   }
}
